package top.vanchor.myblog2back.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.entity.BlogDealRecord;
import top.vanchor.myblog2back.entity.BlogFeedback;
import top.vanchor.myblog2back.entity.BlogReport;
import top.vanchor.myblog2back.service.IBlogDealRecordService;
import top.vanchor.myblog2back.service.IBlogFeedbackService;
import top.vanchor.myblog2back.utils.JWTUtils;
import top.vanchor.myblog2back.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/3/27 17:27
 */
@RestController
@RequestMapping("/admin/feedBackAdvice")
public class FeedBackAdviceController {

    @Resource
    private IBlogFeedbackService service;

    @Resource
    private IBlogDealRecordService dealRecordService;


    /**
     * 获取建议信息
     * @param pageNo 页码
     * @param pageSize 数量
     * @param status 处理状态
     * @return
     */
    @GetMapping("/getList")
    @ResponseBody
    public JSONObject getList(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("status") Integer status
    ){
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonTemp = new JSONObject();
        PageInfo<BlogFeedback> list = new PageInfo<>();
        try {
            list = service.getList(pageNo,pageSize,status);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            jsonTemp.put("list",list.getList());
            jsonTemp.put("total",list.getTotal());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，请求失败");
        }
        return jsonObject;
    }

    @ApiOperation("管理员处理反馈内容")
    @PostMapping("/doEdit")
    @ResponseBody
    public JSONObject doEdit(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        boolean isPass = Boolean.parseBoolean(String.valueOf(data.get("code")));
        Integer feedbackId = Integer.parseInt(String.valueOf(data.get("feedbackId")));
        String resultMsg = String.valueOf(data.get("resultMsg"));
        String updateTime = String.valueOf(System.currentTimeMillis()/1000);
        //获取需要受理的entity
        BlogDealRecord dealRecord = new BlogDealRecord();
        try {
            BlogFeedback feedback = service.getById(feedbackId);
            dealRecord = prepareBean(feedback,resultMsg,isPass,updateTime);
            //后台查重，判断是否已经受理过了
            if (dealRecordService.getCounts(dealRecord) > 0){
                jsonObject.put("code",510);
                jsonObject.put("msg","该请求已被处理，请勿重复操作！");
                return jsonObject;
            }
            if (dealRecordService.toAddItem(dealRecord) > 0){
                int ret = 0;
                feedback.setFeedbackStatus(isPass?1:2);
                feedback.setUpdateTime(updateTime);
                feedback.setUpdateTime(updateTime);
                feedback.setAdminId(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo()))));
                service.toDeal(feedback);
                jsonObject.put("code",200);
                if (isPass){
                    jsonObject.put("msg","受理完成，已采纳用户建议");
                }else{
                    jsonObject.put("msg","受理完成，已驳回用户建议");
                }
            }else{
                jsonObject.put("code",500);
                jsonObject.put("msg","服务器异常，请求失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，请求失败");
        }
        return jsonObject;
    }
    public BlogDealRecord prepareBean(BlogFeedback feedback,String resultMsg,boolean isPass,String updateTime){
        BlogDealRecord dealRecord = new BlogDealRecord();
        dealRecord.setDealType(1);
        dealRecord.setHandleUserId(feedback.getUserId());
        dealRecord.setTargetBody(feedback.getFeedbackContent());
        dealRecord.setFinishTime(updateTime);
        dealRecord.setHandleTime(feedback.getCreateTime());
        dealRecord.setResultMsg(resultMsg);
        dealRecord.setAdminId(Integer.parseInt(Objects.requireNonNull(JWTUtils.getUserId(JwtInterceptor.getTokenInfo()))));
        dealRecord.setResultStatus(isPass?1:2);
        return dealRecord;
    }
}
